package com.face.receive.jsu;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONException;
import org.json.JSONObject;

import com.face.common.jsu.DeletePhoto;
import com.face.operate.jsu.Prestore;
@WebServlet("/DeletePrestore")
public class DeletePrestore extends HttpServlet {

	String saveDir = "/root/prestore/";
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doPost(request, response);
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		
		int id =  Integer.parseInt(request.getParameter("id"));
		Prestore prestore = new Prestore();
		//删除某一合集同时将此合集下的照片一并删除
		List<String> list = prestore.selectFilepathById(id);
		//因为name表的id值是picture表id值得外键，且为cascad,故只需删除主表数据即可
		prestore.deletePreById(id);
		for(String filepath:list){
			DeletePhoto.deletePhoto(saveDir+filepath);
		}
		
		JSONObject json = new JSONObject();
		try {
			json.put("message", "ok");
		} catch (JSONException e) {
			e.printStackTrace();
		}
		response.setContentType("application/json; charset=utf-8");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.write(json.toString());
		System.out.println("返回的json数据：" + json.toString());
	}

}
